package zuo.from0516.basic2020.class01;

public class Code03_InsertionSort {

    public static void insertionSort(int[] arr){
        if(arr == null || arr.length <2){
            return;
        }
        for (int i = 1; i < arr.length; i++) {
            for (int j = i-1; j >=0 && arr[j+1]<arr[j] ; j--) {
                swap(arr,j,j+1);
            }
        }
    }
    public static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i]= arr[j];
        arr[j]=temp;
    }

    public static void main(String[] args) {
        int[] arr = {1,-3,-2,4,2,5,3,8};
        insertionSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}
